﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Core;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Popups;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace App43
{
    public sealed partial class MainPage : Page
    {
        private WeakReference _reference = default(WeakReference);
        private Window _window = default(Window);
        public MainPage()
        {
            this.InitializeComponent();
        }

        private async void btnShowWindow_Click(object sender, RoutedEventArgs e)
        {
            this._window = await typeof(OtherWindow).Show();
            _reference = _reference ?? new WeakReference(new OtherPageViewModel());
            await this._window.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
            {
                ((this._window.Content as Frame).Content as OtherWindow).DataContext = _reference.Target;
            });
        }

        private async void btnCheckIsAlive_Click(object sender, RoutedEventArgs e)
        {
            GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced, true);
            if (_reference == null)
                await new MessageDialog(false.ToString(), "Is it alive?").ShowAsync();
            else
                await new MessageDialog(_reference.IsAlive.ToString(), "Is it alive?").ShowAsync();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            if (_window != null)
                _window.SpecialClose();
        }
    }
}
